package oracle.apps.financials.collection.activity;

import oracle.apps.financials.collection.LoadDataAsyncTask;
import oracle.apps.financials.collection.R;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.AsyncTask.Status;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class WebServiceActivity extends BaseListActivity {

	static final String namespace = "http://collection.financials.apps.oracle/";

	static final String serviceURL = "http://adc6260336.us.oracle.com:7101/Collection-Project-context-root/DataLoaderServicePort";

	final String methodName = "getESSBaseCubeData";

	final int sessionID = 111111;
	protected LoadDataAsyncTask loadDataAsyncTask;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Button webServiceButton = (Button) this.findViewById(R.id.web_service);

		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog()
				.penaltyDeath().build());
		webServiceButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				try {
					doCallWebService();
				} catch (Exception e) {

					e.printStackTrace();

					Toast.makeText(WebServiceActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
				}
			}

		});

		loadDataAsyncTask = new LoadDataAsyncTask(this, null,R.string.loading, R.string.loading_fail, getDatabaseHelper());
		
		Button loadDataButton = (Button) this.findViewById(R.id.load_data);

		loadDataButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				if (Status.RUNNING!=loadDataAsyncTask.getStatus()) {
					Toast.makeText(WebServiceActivity.this,"Rebuild DB...", Toast.LENGTH_SHORT).show();
					loadDataAsyncTask.execute("Rebuild");
				}else{
					Toast.makeText(WebServiceActivity.this,"LoadDataAsyncTask is running !", Toast.LENGTH_SHORT).show();
				}
			}

		});

	}

	private void doCallWebService() throws Exception {
		HttpTransportSE transport = new HttpTransportSE(serviceURL);
		transport.debug = true;

		SoapObject soapObject = new SoapObject(namespace, methodName);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);

		envelope.bodyOut = transport;

		envelope.setOutputSoapObject(soapObject);

		transport.call(null, envelope);
		Object response = envelope.getResponse();

		SoapObject sb = (SoapObject) envelope.bodyIn;

		if (sb != null) {
			String xmlMessage = sb.toString();
			System.out.println(xmlMessage);
			Toast.makeText(this, "XML:" + xmlMessage, Toast.LENGTH_SHORT).show();
		} else {
			Toast.makeText(this, "fail", Toast.LENGTH_SHORT).show();
		}
	}
}